package com.ruoyi.swla2.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.swla2.domain.Terminals;
import com.ruoyi.swla2.mapper.TerminalsMapper;
import com.ruoyi.swla2.model.MultiTerminalDataModel;
import com.ruoyi.swla2.model.TerminalsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.swla2.mapper.TerminalHistoryDataMapper;
import com.ruoyi.swla2.domain.TerminalHistoryData;
import com.ruoyi.swla2.service.ITerminalHistoryDataService;

/**
 * 大表-历史数据Service业务层处理
 *
 * @author gdqt
 * @date 2023-09-11
 */
@Service
public class TerminalHistoryDataServiceImpl extends ServiceImpl<TerminalHistoryDataMapper,TerminalHistoryData> implements ITerminalHistoryDataService {
    @Autowired
    private TerminalHistoryDataMapper terminalHistoryDataMapper;

    @Autowired
    private TerminalsMapper terminalsMapper;


    /**
     * 查询大表-历史数据
     *
     * @param TerminalID 大表-历史数据主键
     * @return 大表-历史数据
     */
    @Override
    public TerminalHistoryData selectTerminalHistoryDataByTerminalID(String TerminalID) {
        return terminalHistoryDataMapper.selectTerminalHistoryDataByTerminalID(TerminalID);
    }

    /**
     * 查询大表-历史数据列表
     *
     * @param terminalHistoryData 大表-历史数据
     * @return 大表-历史数据
     */
    @Override
    public List<TerminalHistoryData> selectTerminalHistoryDataList(TerminalHistoryData terminalHistoryData) {
        return terminalHistoryDataMapper.selectTerminalHistoryDataList(terminalHistoryData);
    }

    /**
     * 新增大表-历史数据
     *
     * @param terminalHistoryData 大表-历史数据
     * @return 结果
     */
    @Override
    public int insertTerminalHistoryData(TerminalHistoryData terminalHistoryData) {
        return terminalHistoryDataMapper.insertTerminalHistoryData(terminalHistoryData);
    }

    /**
     * 修改大表-历史数据
     *
     * @param terminalHistoryData 大表-历史数据
     * @return 结果
     */
    @Override
    public int updateTerminalHistoryData(TerminalHistoryData terminalHistoryData) {
        return terminalHistoryDataMapper.updateTerminalHistoryData(terminalHistoryData);
    }

    /**
     * 批量删除大表-历史数据
     *
     * @param TerminalIDs 需要删除的大表-历史数据主键
     * @return 结果
     */
    @Override
    public int deleteTerminalHistoryDataByTerminalIDs(String[] TerminalIDs) {
        return terminalHistoryDataMapper.deleteTerminalHistoryDataByTerminalIDs(TerminalIDs);
    }

    /**
     * 删除大表-历史数据信息
     *
     * @param TerminalID 大表-历史数据主键
     * @return 结果
     */
    @Override
    public int deleteTerminalHistoryDataByTerminalID(String TerminalID) {
        return terminalHistoryDataMapper.deleteTerminalHistoryDataByTerminalID(TerminalID);
    }

    @Override
    public List<TerminalHistoryData> selectByParamMap(Map<String, Object> paramMap) {
        return terminalHistoryDataMapper.selectByParamMap(paramMap);
    }

    @Override
    public List<MultiTerminalDataModel> getMultiTerminalDataList(TerminalsModel model) {
        //返回数组
        List<MultiTerminalDataModel> resList = new ArrayList<>();
        if(model.getTerminalIds() == null || model.getTerminalIds().isEmpty()){
            return resList;
        }
        //压力表ID
        List<String> terminalIds = model.getTerminalIds();
        String terminalId1 = terminalIds.get(0);
        String terminalId2 = terminalIds.get(1);
        //压力表基础信息
        List<Terminals> terminalsList = terminalsMapper.selectList(new LambdaQueryWrapper<Terminals>()
                .in(Terminals::getTerminalID,terminalIds));
        //terminalID分组对象
        Map<String,Terminals> terminalsMap = terminalsList.stream().collect(Collectors.toMap(Terminals::getTerminalID,Function.identity()));
        List<TerminalHistoryData> list = terminalsMapper.selectMultiTerminalData(model);
        if(!list.isEmpty()){
            //时间数组
            List<Date> timeList = list.stream().map(TerminalHistoryData::getReadDate).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            //根据时间分组
            Map<Date,List<TerminalHistoryData>> dateListMap = list.stream().collect(Collectors.groupingBy(TerminalHistoryData::getReadDate));
            for (Date date : timeList) {
                MultiTerminalDataModel tempData = new MultiTerminalDataModel();
                List<TerminalHistoryData> terminalHistoryDataList = dateListMap.get(date);
                for (TerminalHistoryData terminalHistoryData : terminalHistoryDataList) {
                    //设备A
                    if(terminalHistoryData.getTerminalID().equals(terminalId1)){
                        tempData.setTerminalId1(terminalHistoryData.getTerminalID());
                        tempData.setTerminalName1(terminalHistoryData.getTerminalName());
                        tempData.setPressure1(terminalHistoryData.getPressure());
                    }
                    //设备B
                    if(terminalHistoryData.getTerminalID().equals(terminalId2)){
                        tempData.setTerminalId2(terminalHistoryData.getTerminalID());
                        tempData.setTerminalName2(terminalHistoryData.getTerminalName());
                        tempData.setPressure2(terminalHistoryData.getPressure());
                    }
                }
                if(!StringUtils.isEmpty(tempData.getTerminalId1()) && !StringUtils.isEmpty(tempData.getTerminalId2())){
                    BigDecimal bd = BigDecimal.valueOf(tempData.getPressure1().doubleValue() - tempData.getPressure2().doubleValue()).setScale(2, RoundingMode.HALF_UP);
                    tempData.setValue(bd);
                } else if (StringUtils.isEmpty(tempData.getTerminalId1())) {
                    tempData.setTerminalId1(terminalId1);
                    tempData.setTerminalName1(terminalsMap.get(terminalId1).getTerminalName());
                } else if (StringUtils.isEmpty(tempData.getTerminalId2())) {
                    tempData.setTerminalId2(terminalId2);
                    tempData.setTerminalName2(terminalsMap.get(terminalId2).getTerminalName());
                }
                tempData.setDataTime(DateUtil.formatDateTime(date));
                resList.add(tempData);
            }
        }
        return resList;
    }

    @Override
    public AjaxResult getMultiTerminalDataChart(TerminalsModel model) {
        Map<String, Object> resMap = new HashMap<>();
        List<String> chartTimeList = new ArrayList<>();
        List<BigDecimal> pressList1 = new ArrayList<>();
        List<BigDecimal> pressList2 = new ArrayList<>();
        //压力表ID
        List<String> terminalIds = model.getTerminalIds();
        if(model.getTerminalIds() != null && !model.getTerminalIds().isEmpty()){
            String terminalId1 = terminalIds.get(0);
            String terminalId2 = terminalIds.get(1);
            List<TerminalHistoryData> list = terminalsMapper.selectMultiTerminalData(model);
            //时间数组
            List<Date> timeList = list.stream().map(TerminalHistoryData::getReadDate).distinct().sorted(Date::compareTo).collect(Collectors.toList());
            //根据时间分组
            Map<Date,List<TerminalHistoryData>> dateListMap = list.stream().collect(Collectors.groupingBy(TerminalHistoryData::getReadDate));
            for (Date date : timeList) {
                chartTimeList.add(DateUtil.formatDateTime(date));

                TerminalHistoryData t1 = dateListMap.get(date).stream().filter(terminalHistoryData -> terminalHistoryData.getTerminalID().equals(terminalId1)).findFirst().orElse(null);
                TerminalHistoryData t2 = dateListMap.get(date).stream().filter(terminalHistoryData -> terminalHistoryData.getTerminalID().equals(terminalId2)).findFirst().orElse(null);
                if(t1 != null){
                    pressList1.add(t1.getPressure());
                }else {
                    pressList1.add(null);
                }
                if(t2 != null){
                    pressList2.add(t2.getPressure());
                }else {
                    pressList2.add(null);
                }
            }
        }
        resMap.put("timeList",chartTimeList);
        resMap.put("pressList1",pressList1);
        resMap.put("pressList2",pressList2);
        return AjaxResult.success(resMap);
    }
}
